from adm.views import room_list
from adm.models import trans
import datetime
from django.db.models import Q
from django.shortcuts import render
from common.models import Detail
from scheduler.schedule import sche
from django.urls import path
from django.http import HttpResponse, JsonResponse
import datetime
import time
import json
# Create your models here.
# 账单
class Bill():
    def __init__(self, roomid):
        index = trans(roomid)
        print(index)
        self.RoomId = roomid
        request = sche.get_request(roomid)
        self.Total_Fee = room_list[index].totalCost
        self.date_in = room_list[index].checkinT
        self.date_out = datetime.datetime.now()
    # 前台


class Reception():
    def __init__(self):
        self.user = "rjgc"
        self.passwd = "rjgc"
    def login(self, user, passwd):
        if user == self.user and passwd == self.passwd:
            return 0
        else:
            return 1


    def create_bill(self, roomid):
        bill = Bill(roomid)
        res = dict()
        res["data"] = dict()
        res["error_code"] = 0
        res["data"]["RoomId"] = roomid
        res["data"]["Total_Fee"] = bill.Total_Fee
        res["data"]["date_in"] = bill.date_in
        res["data"]["date_out"] = bill.date_out
        return res

    def create_detail(self, roomid):
        res = dict()
        res["data"] = dict()
        res["error_code"] = 0
        detail = Detail.objects.filter(rid=roomid).order_by('-id')[:1]
        res["data"]["RoomId"] = roomid
        res["data"]["RequestTime"] = detail[0].lastinTime
        res["data"]["RequestDuration"] = detail[0].requestDuration
        res["data"]["FanSpeed"] = detail[0].fanSpeed
        res["data"]["FeeRate"] = detail[0].feeRate
        res["data"]["fee"] = detail[0].fee
        return res
# 前台控制器



def log_in(request):
    request.params = json.loads(request.body)
    user = request.params["user"]
    passwd = request.params["passwd"]
    res = dict()
    res["error_code"] = recep.login(user, passwd)
    print(res["error_code"])
    return JsonResponse(res)


def create_bill(request):
    request.params = json.loads(request.body)
    roomId = request.params["roomId"]
    res = recep.create_bill(roomId)
    return JsonResponse(res)


def create_detail(request):
    request.params = json.loads(request.body)
    roomId = request.params["roomId"]
    res = recep.create_detail(roomId)
    return JsonResponse(res)

recep = Reception()

urlpatterns = [
    path('signup/', log_in),
    path('createinvoice/', create_bill),
    path('createrd/', create_detail),
]
